package cn.it.trigger.http;

import cn.it.infrastructure.api.IUserApi;
import cn.it.infrastructure.api.dto.*;
import cn.it.infrastructure.domain.service.IUserService;
import cn.it.infrastructure.domain.valobj.AddressVO;
import cn.it.infrastructure.domain.valobj.BuyVO;
import cn.it.infrastructure.domain.valobj.OrderVO;
import cn.it.infrastructure.po.TbGoods;
import cn.it.infrastructure.po.TbUser;
import cn.it.infrastructure.type.enums.ResponseCode;
import cn.it.infrastructure.type.exception.AppException;
import cn.it.infrastructure.type.model.Response;
import cn.it.infrastructure.domain.valobj.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mzq
 * @description
 * @createTime 2024/7/5
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/${app.config.api-version}/user")
public class UserApiController implements IUserApi {

    private IUserService userService;

    public UserApiController(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "logon", method = RequestMethod.POST)
    @Override
    public Response<UserServiceResponseDTO> wxLogin(@RequestBody UserServiceRequestDTO request) {
        try {
            LoginVO loginVO = LoginVO.builder()
                    .nickName(request.getNickName())
                    .openId(request.getOpenId())
                    .imageUrl(request.getImageUrl())
                    .build();

            TbUser user = userService.wxLogin(loginVO);
            log.info("用户登录或校验成功:{}", user.getNickName());

            return Response.<UserServiceResponseDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(UserServiceResponseDTO.builder().
                            id(user.getId())
                            .nickName(user.getNickName())
                            .openId(user.getOpenId())
                            .imageUrl(user.getImageUrl())
                            .sessionKey(user.getSessionKey())
                            .token(user.getToken())
                            .address(user.getAddress())
                            .phone(user.getPhone())
                            .status(user.getStatus())
                            .userRights(user.getUserRights())
                            .description(user.getDescription())
                            .build())
                    .build();
        } catch (Exception e) {

            return Response.<UserServiceResponseDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }


    }

    /**
     * 用户下单
     *
     * @param request
     * @return curl --request POST \
     * --url http://localhost:8000/api/v1/user/order \
     * --header 'content-type: application/json' \
     * --data '{
     * "goodsId":"5,10,24",
     * "userId": 10
     * }'
     */
    @RequestMapping(value = "order", method = RequestMethod.POST)
    @Override
    public Response<UserPlacesOrderResponseDTO> placeOrder(@RequestBody UserPlacesOrderRequestDTO request) {
        try {
            BuyVO buyVO = BuyVO.builder()
                    .goodsId(request.getGoodsId())
                    .openId(request.getOpenId())
                    .build();
            OrderVO orderVO = userService.placeOrder(buyVO);

            log.info("下单成功 :{}", orderVO.getId());

            return Response.<UserPlacesOrderResponseDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(UserPlacesOrderResponseDTO.builder()
                            .id(orderVO.getId())
                            .goodsId(orderVO.getGoodsId())
                            .userId(orderVO.getUserId())
                            .price(orderVO.getPrice())
                            .orderContent(orderVO.getOrderContent())
                            .build())
                    .build();
        } catch (AppException e) {
            return Response.<UserPlacesOrderResponseDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "favorites", method = RequestMethod.POST)
    @Override
    public Response<List<GoodsServiceResponseDTO>> userFavorites(@RequestBody UserAdnGoodsDTO request) {
        try {
            Long id = request.getId();

            List<TbGoods> goodsList = userService.userFavorites(id);

            List<GoodsServiceResponseDTO> responseList = new ArrayList<>(goodsList.size());

            for (TbGoods tbGoods : goodsList) {
                GoodsServiceResponseDTO response = GoodsServiceResponseDTO.builder()
                        .name(tbGoods.getName())
                        .realPrice(tbGoods.getRealPrice())
                        .discountPrice(tbGoods.getDiscountPrice())
                        .imageUrl(tbGoods.getImageUrl())
                        .norms(tbGoods.getNorms())
                        .stock(tbGoods.getStock())
                        .description(tbGoods.getDescription())
                        .isShow(tbGoods.getIsShow())
                        .build();
                responseList.add(response);
            }
            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseList)
                    .build();
        }catch (AppException e){

        }
        return Response.<List<GoodsServiceResponseDTO>>builder()
                .code(ResponseCode.UN_ERROR.getCode())
                .info(ResponseCode.UN_ERROR.getInfo())
                .build();

    }

    @RequestMapping(value = "address", method = RequestMethod.POST)
    @Override
    public Response modifyAddress(@RequestBody UserServiceRequestDTO request) {
        try {
            AddressVO addressVO = AddressVO.builder()
                    .openId(request.getOpenId())
                    .address(request.getAddress())
                    .phone(request.getPhone())
                    .build();

            userService.updateAddress(addressVO);

            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (AppException e){
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }

}
